package com.huaxia.controller;

import com.alibaba.fastjson.JSON;
import com.huaxia.constant.ResultConstant;
import com.huaxia.exception.BusiException;
import com.huaxia.model.Order;
import com.huaxia.model.OrderPage;
import com.huaxia.model.ResultInfo;
import com.huaxia.service.OrderService;
import com.huaxia.util.AssertUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/5.
 */
@RestController
@RequestMapping("/order")
public class MainController {

    private static final Log logger = LogFactory.getLog(MainController.class);

    @Resource
    private OrderService orderService;

    /**
     * 提交预约
     * @param order
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public ResultInfo add(@RequestBody Order order) throws Exception {
       logger.info("收到参数："+order.toString());
        ResultInfo resultInfo = null;
        try {
             resultInfo = orderService.insertSelective(order);
        } catch (Exception e) {
            throw new BusiException(ResultConstant.OPS_FAILED_MSG);
        }
        return resultInfo;
    }

    /**
     * 根据条件查询预约 手机号 ，预约码
     * @param order
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public ResultInfo query(@RequestBody OrderPage order) throws Exception {
        logger.info("收到参数："+order.toString());
        ResultInfo resultInfo = orderService.queryOrderList(order);
        return resultInfo;

    }

    /**
     * 查询 时间和场次条件
     * @param params
     * @return
     * @throws Exception
     */

    @PostMapping("/queryCondition")
    public ResultInfo queryCondition(@RequestBody String params) throws Exception {
        logger.info("收到参数："+params);
        Map<String, String> mapValue = (Map) JSON.parse(params);
        String orderDate = null;
        if (mapValue != null || mapValue.get("orderDate") != null ) {
            orderDate = mapValue.get("orderDate");
        }
        ResultInfo resultInfo = orderService.queryCondtion(orderDate);
        return resultInfo;

    }

    /**
     * 签到
     * @param order
     * @return
     * @throws Exception
     */
    @PostMapping("/signIn")
    public ResultInfo signIn(@RequestBody Order order) throws Exception {
        System.out.println("收到参数："+order.toString());
        ResultInfo resultInfo = orderService.signIn(order);
        return resultInfo;
    }

    /**
     * 统计数据
     * @param params
     * @return
     * @throws Exception
     */
    @PostMapping("/statistic")
    public ResultInfo statistic(@RequestBody String params) throws Exception {
        System.out.println("收到参数："+params);
        Map<String, String> mapValue = (Map) JSON.parse(params);
        //type = "O"  按日期统计  type="1" 按来源统计 type="2" 按每天 个来源统计 默认就按日期统计
       String type = "";
       if (mapValue != null && mapValue.get("type") != null) {
           type = mapValue.get("type").toString();
       }
        ResultInfo resultInfo = orderService.statistic(type);
        return resultInfo;
    }

}
